package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import ModelLayer.*; // tjek
/**
 * User interface class to the AddressBook
 * 
 * @author (team 1)
 * 
 * To CRUD for persons
 */
public class AddressBookUI
{   

    private AddressCtr addrCtr;//reference to the ControlLayer

    //constructor which does nothing
    public AddressBookUI()
    {
        addrCtr = new AddressCtr();
    }
    /**
     * Starts the menu for the addressBookUI
       */
    public void start()
    {
        addressMenu();
    }
    /**
     * method for showing the menu
       */
    public void addressMenu()
    {
        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            int choice = writeAddressBookMenu(); 
            if(choice == 1)
            {   //show person
                printPerson();
            }
            else{
                if(choice == 2){
                    //create person
                    createPerson();
                }  
                else{
                    if(choice == 3){
                        deletePerson();
                    }  
                    else{
                        if(choice == 4){
                            // update person
                            updatePerson();
                        }
                        else{
                            if(choice == 5){
                                listAllPerson();
                            }
                            else{
                                exit = true;
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else

        }//end while
    }
    /**
     * Shows the possible choice to answer and takes a input and returns a int
       */
    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" *** AddressBook Menu ***");
        System.out.println(" (1) Show information about person");
        System.out.println(" (2) Add person");
        System.out.println(" (3) Delete person");
        System.out.println(" (4) Update person");
        System.out.println(" (5) List all persons");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");

        int choice = keyboard.nextInt();
        return choice;
    }
    /**
     * createPerson
     */
    public void createPerson()
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter id: ");
        long id = input.nextLong();
        System.out.println("Enter name: ");
        String name = input.next();
        System.out.println("Enter address: ");
        String address = input.next();
        System.out.println("Enter Postal Code: ");
        String postalCode = input.next();
        System.out.println("Enter City: ");
        String city = input.next();
        System.out.println("Enter Phone No.: ");
        String phone = input.next();

        addrCtr.createPerson(id, name, address, postalCode, city, phone);
    }
    /**
     * Updates a person
       */
    public void updatePerson()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter id: ");
        long pID = input.nextLong();
        Person p = addrCtr.findPerson(pID);
        if(p != null)
        {
            System.out.println("Enter name: ");
            String name = input.next();
            System.out.println("Enter address: ");
            String address = input.next();
            System.out.println("Enter Postal Code: ");
            String postalCode = input.next();
            System.out.println("Enter City: ");
            String city = input.next();
            System.out.println("Enter Phone No.: ");
            String phone = input.next(); 

            addrCtr.updatePerson(pID, name, address, postalCode, city, phone);
        }
    }

    /**
     * Reads an ID entered by the user, and finds
     * information about the assosicated person
     */
    private Person getPerson()
    {//is to be changed when the address book controller is completed    
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write id: ");
        long id = keyboard.nextInt();

        Person aPerson = addrCtr.findPerson(id);
        
        if(aPerson != null)
        {
            return aPerson;
        }

        else
        {
            return null;
        }
    }
    /**
     * Prints a person
       */
    public void printPerson()
    {
        Person aPerson = getPerson();

        System.out.println("Id: " + aPerson.getId());
        System.out.println("Name: " + aPerson.getName());
        System.out.println("Phonenumber: " + aPerson.getPhone());
    }
    /**
     * Lists all persons
       */
    public void listAllPerson() {
        addrCtr.listAllPersons();
    }
    /**
    * Deletes a person
      */
    public void deletePerson()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write id: ");
        long id = keyboard.nextInt();

        addrCtr.deletePerson(id);     
    }

}
